package com.ruoyi.framework.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class JSONUtil {


	private static ObjectMapper objMapper = new ObjectMapper();
	static {
		objMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		objMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		//此方法可以直接屏蔽掉值为null的参数
		objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
	}

	/**
	 * 把一个对象转为JSON字符串
	 * @param obj 任何一个对象
	 * @return
	 */
	public static String toJSONString(Object obj){
		return JSON.toJSONString(obj);
	}
	
	/**
	 * 把JSON字符串转为Map对象
	 * @param jsonStr JSON字符串
	 * @return Map对象
	 */
	@SuppressWarnings("unchecked")
	public static Map parseJSONToMap(String jsonStr){
		return JSON.parseObject(jsonStr, Map.class);
	}
	
	/**
	 * 把JSON数组字符串转为List对象
	 * @param jsonArrayStr JSON数组字符串
	 * @return Map对象
	 */
	@SuppressWarnings("unchecked")
	public static List parseJSONToList(String jsonArrayStr){
		List list = new ArrayList();
		JSONArray array = JSON.parseArray(jsonArrayStr);
		for(int index = 0;index < array.size();index ++){
			list.add(array.get(index));
		}
		return list;
	}
	
	/**
	 * 把JSON数组字符串转为String[]字符串对象
	 * @param jsonArrayStr JSON数组字符串
	 * @return Map对象
	 */
	public static String[] parseJSONToArray(String jsonArrayStr){
		
		JSONArray array = JSON.parseArray(jsonArrayStr);
		String[] strArray = new String[array.size()];
		for(int index = 0;index < array.size();index ++){
			strArray[index] = array.get(index).toString();
		}
		return strArray;
	}

	/**
	 * json字符串转成 实体
	 * @param clazz 实体类名
	 * @param json json字符串
	 * @param <T> 实体类
	 * @return
	 */
	public static <T> T from(Class<T> clazz,String json) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
		try {
			return mapper.readValue(json,clazz);
		} catch (IOException e) {
			log.error("", e);
		}

		return null;
	}

	/**
	 * 实体转成字符串
	 * @param obj 实对对象
	 * @return
	 */
	public static String toString(Object obj) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

			return mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
            log.error("", e);
		}

		return "";
	}


	public static <T> T readValue(String value, TypeReference<T> ref) {
		try {
			return objMapper.readValue(value, ref);
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static <T> T readValue(String value,Class<T> ref) {
		try {
			return objMapper.readValue(value, ref);
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static String write(Object obj) {
		try {
			return objMapper.writeValueAsString(obj);
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static String prettyWrite(Object obj) {
		try {
			return objMapper.writer(SerializationFeature.INDENT_OUTPUT).writeValueAsString(obj);
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
